<template>
  <div>
    <el-dialog
      :title="title"
      v-model="visable"
      width="35%"
      :close-on-click-modal="false"
      :draggable="true"
      style="max-width: 450px;"
    >
      <div class="bs-dialog">
        <el-form
          ref="addRef"
          :model="dialogFormData"
          :rules="dialogFormRules"
          label-width="auto"
          style="width: 95%;padding-left: 5%;text-align: left;"
        >
        <el-form-item label="练习账号:" prop="studentID">
          <el-select
            filterable
            v-model="dialogFormData.studentID"
            placeholder="请选择练习账号" 
            clearable
          >
            <el-option
              v-for="item in studentList"
              :key="item.id"
              :label="item.code+'-'+item.wechatNickname+'-'+item.phone"
              :value="item.id"
            >
                <div>
                 <div style="display: inline-block;width: 200px;">
                  <span title="学员编号">{{ item.code }}</span>
                  <span title="微信昵称" style="float:right;color:#c7c7c7;margin-right: 5px;">{{ item.wechatNickname }}</span>
                 </div>
                 <span title="手机号码" style="display: inline-block;color: var(--el-color-primary);width: 90px;">{{ item.phone }}</span>
                </div>
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="出入金日期:" prop="tradeDate">
          <el-date-picker
            v-model="dialogFormData.tradeDate"
            type="date"
            placeholder="请选择练习日期"
            size="small"
            value-format="YYYY-MM-DD"
          />
        </el-form-item>
        <el-form-item label="出/入金:" prop="inoutFlag">
          <el-select v-model="dialogFormData.inoutFlag" placeholder="请选择出/入金" collapse-tags clearable>
            <template v-for="item in capitalTypeList" :key="item.id">
              <el-option :label="item.dicName" :value="item.dicValue" />
            </template>
          </el-select>
        </el-form-item>
        <el-form-item label="金额:" prop="amount">
          <el-input-number v-model="dialogFormData.amount" placeholder="请输入金额"  clearable/>
        </el-form-item>
        <el-form-item label="备注:" prop="remark">
          <el-input v-model="dialogFormData.remark" type="textarea" :rows="4" placeholder="请输入备注"  clearable />
        </el-form-item>
      </el-form>
      </div>
      <template #footer>
        <div class="dialog-footer" style="text-align: right;margin-right: 7%;">
          <el-button  size="small" @click="visable = false"> 取消 </el-button>
          <el-button  size="small" type="primary" @click="save"> 保存 </el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { selectDictionaryByTypeCode } from '@/api/system/dictionary.js'
import { updateItem, addItem, getItemByID } from '@/api/oss/capitalManage.js'
import { getAll } from '@/api/oss/student.js'
// (修改和新增共用一个弹窗时,this.$refs['xxx'].resetFields()函数失效, 因此使用这种方式重置表单数据)
let orgDialogFormData = {
        id: undefined,
        account:"",
        tradeDate:"",
        inoutFlag:"",
        studentID:"",
        remark:"",
      }
export default {
  data() {
    return {
      title:"",
      visable: false,
      dialogFormData: { ...orgDialogFormData},
      dialogFormRules: {
        account: [
          { required: true, message: '请选择学员信息', trigger: ['blur', 'change'] },
        ]
      },
      // 学员列表
      studentList:[],
      // 期权类型列表
      optionTypeList:[],
      // 资金类型列表
      capitalTypeList:[],
    }
  },
  methods: {
    openModal(id) {
      console.log("openModal=",id);
      this.visable = true
      this.$nextTick(() => {
        this.dialogFormData = {...orgDialogFormData}
        if (this.isNotBlank(id)) {
          this.title = '修改资金信息'
          // 获取修改的信息
          getItemByID({ 'id': id }).then(res => {
            console.log("修改返回值=",res);
            if (res.code === 1) {
              this.dialogFormData = {...res.data}
            }
          })
        } else {
          this.title = '新增资金信息'
        }
        selectDictionaryByTypeCode({"dicTypeCode":"CapitalType"}).then(res=>{
          if (res.code == 1) {
            // 取资金管理的资金类型
            let list = res.data.filter(item=>{
              let type = Number(item.dicValue)
              return type >= 0 && type <= 9
            })
            console.log("---获取类型=",list);
            this.capitalTypeList = list
          }
        })
        // 获取所有学员信息
        getAll({}).then(res=>{
          if (res.code == 1) {
            this.studentList = res.data
          }
        })
      })
    },
    
    // 保存
    save() {
      this.$refs['addRef'].validate((valid) => {
        if (valid) {
          if (this.isBlank(this.dialogFormData.id)) {
            addItem(this.dialogFormData).then(res => {
              if (res.code == 1) {
                this.$message.success('操作成功')
                this.visable = false
                // 刷新父组件中的表格数据
                this.$emit('flushTable', {})
              } else {
                this.$message.error(res.msg)
              }
            }).catch((error) => {
            })
          } else {
            updateItem(this.dialogFormData).then(res => {
              if (res.code == 1) {
                this.$message.success('操作成功')
                this.visable = false
                // 刷新父组件中的表格数据
                this.$emit('flushTable', {})
              } else {
                this.$message.error(res.msg)
              }
            }).catch((error) => {
            })
          }
          

        }
      })
    },

  }
}
</script>
<style scoped>
  :deep(.el-form-item__content) {
    display: inline-block;
    text-align: left;
    padding-right: 10%;
  }
  :deep(.el-drawer__title) {
    text-align: left;
  }
  :deep(.el-input),
  :deep(.el-input-number--small),
  :deep(.el-select) {
    width: 100%;
  }
</style>
